package cn.epsilon3.interceptor;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.hutool.core.util.ObjectUtil;
import cn.epsilon3.configBean.InitializingSecurityConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 拦截器注册
 */
@Configuration
public class SecurityInterceptorsConfiguration extends WebMvcConfigurationSupport {

    @Resource
    private InitializingSecurityConfig initializingSecurityConfig;

    @Resource
    private SecurityTokenInterceptor securityTokenInterceptor;

    @Resource
    private SecurityUrlInterceptor securityUrlInterceptor;

    @Resource
    private SecurityBlackListInterceptor securityBlackListInterceptor;


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePath = initializingSecurityConfig.getExcludePathPatterns();
        List<String> excludePathPatterns = new ArrayList<>();
        excludePathPatterns.add("/swagger-resources/**");
        excludePathPatterns.add("/v2/**");
        excludePathPatterns.add("/api");
        excludePathPatterns.add("/api-docs");
        excludePathPatterns.add("/api-docs/**");
        excludePathPatterns.add("/doc.html/**");
        excludePathPatterns.add("/webjars/**");
        excludePathPatterns.add("/favicon.ico");
        excludePathPatterns.add("/error");
        if (ObjectUtil.isNull(excludePath)) {
            excludePath = excludePathPatterns;
        } else {
            excludePath.addAll(excludePathPatterns);
        }
        registry.addInterceptor(securityBlackListInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePathPatterns);
        registry.addInterceptor(securityTokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath);
        registry.addInterceptor(securityUrlInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath);
        registry.addInterceptor(new SaInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath);
    }
}
